Exercise 1
Choose the correct answer
-
Which statement is used to display output in Python?
a)input()b)print()c)display()d)output()Answer: b)
print() -
Which of the following is NOT a valid Python data type mentioned in
the text?
a) int b) float c) character d) boolAnswer: c) character
-
What type of values does the Boolean data type hold?
a) Whole numbers b) Decimal numbers c) Text d) True or FalseAnswer: d) True or False
-
Which of the following is an example of a relational operator?
a)+b)*c)==d)andAnswer: c)
== -
Which logical operator returns True if both conditions are true?
a)orb)notc)andd)!=Answer: c)
and -
What is the purpose of the if statement in Python?
a) To repeat a block of code.
b) To define a function.
c) To execute a block of code only if a condition is true.
d) To store multiple items in a single variable.Answer: c) To execute a block of code only if a condition is true.
-
Which conditional statement allows you to check multiple conditions in
sequence?
a) if b) if-else c) nested if d) if-elif-elseAnswer: d) if-elif-else
-
What is the term for repeating a block of code multiple times?
a) Selection b) Iteration c) Condition d) AssignmentAnswer: b) Iteration
-
Which type of loop is used when you know the number of times you want
to repeat a block of code?
a) while loop b) for loop c) if loop d) nested loopAnswer: b) for loop
-
Which data structure is used to store multiple items in a single
variable in Python, as shown in the example
["Computer", "Science", 20, True]?
a) String b) Tuple c) List d) DictionaryAnswer: c) List
Write short answers to these questions
-
What is the primary function of the
input()function in Python?It pauses the program, lets the user type something, and hands that typing back as a string.
-
Provide an example of a string literal in Python.
name = "Bhaktapur1" -
What is an identifier in the context of Python programming?
An identifier is the name given to a program unit such as a variable, a function or a class.
-
Explain the difference between the division operator
(
/) and the floor division operator (//) in Python./keeps the decimal part,//throws it away and floors the answer.print(9 / 2) # 4.5print(9 // 2) # 4 -
Define the concept of data types in Python.
A data type is the kind of value a variable holds, such as
int,float,strorbool. -
What is the purpose of relational operators in Python? Give one
example.
They compare two values and answer with
TrueorFalse, like7 > 5which givesTrue. -
Describe the functionality of the else block in an if-else statement.
The else block runs only when the if condition turns out False.
-
What is a nested if statement?
It is an if statement written inside another if statement, so the inner one is only reached when the outer condition is True.
-
When would you typically use a for loop instead of a while loop?
When the number of repetitions is already known, like walking through
range(5)or a list. -
What is the role of the
range()function often used with for loops?It produces the sequence of numbers that the for loop walks over.
-
Define a Python list and provide a simple example.
A list is a built-in type that stores many values in one variable.
marks = [10, 20, 30] -
Discuss the concept of iteration in programming.
Iteration is repeating a task again and again until a stopping condition is satisfied. Python does it with
forandwhileloops.
Write long answers to these questions
-
Explain the different categories of operators available in Python
(arithmetic, relational, and logical) with examples of each.
Arithmetic operators do maths, relational operators compare values and answer True or False, and logical operators join those answers together.
print(10 + 20) # arithmetic, gives 30print(10 > 20) # relational, gives Falseprint(10 > 5 and 3 < 4) # logical, gives True -
Describe the three main types of conditional statements in Python (if,
if-else, and if-elif-else).
ifruns a block only when its condition is True.if-elseadds a fallback block for when it is False.if-elif-elsechecks many conditions in order and runs the first one that is True.marks = int(input("Marks: "))if marks >= 80:print("Distinction")elif marks >= 40:print("Pass")else:print("Fail") -
Explain the two types of loops available in Python (for and while)
with their respective syntaxes and a practical example of each.
A
forloop walks through a known sequence. Awhileloop keeps going as long as its condition stays True.for i in range(1, 4):print("Round", i)count = 1while count <= 3:print("Round", count)count += 1Output
Round 1
Round 2
Round 3
Round 1
Round 2
Round 3 -
Describe the four basic data types covered in the text (integer,
float, string, and boolean), providing an example of each. Why is it
important to understand data types when writing Python programs?
age = 26 # intheight = 6.0 # floatname = "Manish" # stris_student = True # boolTypes decide what an operation means.
"2" + "2"joins text into"22"while2 + 2adds numbers into4, so the wrong type quietly gives the wrong answer or aTypeError. -
Imagine you need to write a Python program that takes an integer input
from the user and performs the following actions: prints "Positive" if
the number is greater than 0, "Negative" if it is less than 0, "Zero"
if it is equal to 0, and if the number is positive, it then checks
whether it is even or odd and prints the result.
num = int(input("Enter a number: "))if num > 0:print("Positive")if num % 2 == 0:print("Even")else:print("Odd")elif num < 0:print("Negative")else:print("Zero")Output
Enter a number: 7
Positive
Odd